<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500"  :close-on-press-escape="mode != 'show' ? false : true"
             destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="登录密码" prop="password">
				<el-input type="password" v-model="form.password" clearable show-password autocomplete="new-password"></el-input>
			</el-form-item>
			<el-form-item label="确认密码" prop="password_confirm">
				<el-input type="password" v-model="form.password_confirm" clearable show-password autocomplete="new-password"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "set_passord",
				titleMap: {
					set_passord: '重置密码',
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					ids:"",
					password: "",
					password_confirm:""
				},
				//验证规则
				rules: {
					password: [
						{required: true, message: '请输入登录密码'},
						{validator: (rule, value, callback) => {
							if (this.form.password_confirm !== '') {
								// this.$refs.dialogForm.validateField('password2');
							}
							callback();
						}}
					],
					password_confirm: [
						{required: true, message: '请再次输入密码'},
						{validator: (rule, value, callback) => {
							if (value !== this.form.password_confirm) {
								callback(new Error('两次输入密码不一致!'));
							}else{
								callback();
							}
						}}
					],
				},
			}
		},
		mounted() {
		},
		methods: {
			//显示
			open(mode='set_passord'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res  = await this.$API.system.admin.setPassword.post(this.form);
						this.isSaveing = false;
						if(res.code == 1){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}else{
						return false;
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form.ids = data.ids
				this.form.password = data.password
				this.form.password_confirm = data.password_confirm
				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				//Object.assign(this.form, data)
			}
		}
	}
</script>

<style>
</style>
